import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/home/HomeView.vue'
import IndexView from '@/views/index/IndexView.vue'
import UserSettingView from '@/views/user/UserSettingView.vue'
import ProfileView from '@/views/user/profile/ProfileView.vue'
import GPTSettingView from '@/views/user/gpt/GPTSettingView.vue'
import { ElMessage } from 'element-plus'

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // {
    //   path: '/',
    //   component: IndexView,
    //   redirect: 'home',
    //   children: [
    //     { path: 'home', name: 'home', component: HomeView },
    //     {
    //       path: 'user/:routeName',
    //       component: UserSettingView,
    //       props: true,
    //       redirect: '/user/profile',
    //       children: [
    //         { path: '', name: 'profile', component: ProfileView },
    //         { path: '', name: 'gpt', component: GPTSettingView }
    //       ]
    //     }
    //   ]
    // },
    {
      path: '/',
      redirect: '/login' , // 默认跳转到登录页
      children: []
      
    },
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login/LoginView.vue')
    },
    {
      path: '/register',
      name: 'register',
      component: () => import('@/views/login/Register.vue')
    },
    
    {
      path: '/RootPage',
      name: 'RootPage',
      component: () => import('@/views/edu/RootPage.vue'),
      children: [
        { path: '', name: 'Home', component: () => import('@/views/home/HomeView.vue')},
        { path: '', name: 'Jiaoan', component: () => import('@/views/home/Jiaoan.vue')},
        { path: '1', name: 'Teach', component: () => import('@/views/edu/TeachPage.vue') },
        { path: '2d', name: 'Teach', component: () => import('@/views/edu/TeachPage.vue')},
        { path: 'effective', name: 'Effective', component: () => import('@/views/edu/EffPage.vue')},
        { path: 'science', name: 'Science', component: () => import('@/views/edu/SciPage.vue')},
        { path: 'gpt', name: 'Gpt', component: () => import('@/views/home/HomeView.vue')},
        { path: '', name: 'profile', component: ProfileView },
            // { path: '', name: 'gpt', component: GPTSettingView }
      ]
    }
  ]
})

router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem('isAuthenticated'); // 假设我们用 localStorage 来存储登录状态
  console.log(to.name)
  console.log(isAuthenticated)
  console.log(!isAuthenticated)
  if (to.name !== 'login' && isAuthenticated === "false") {
    
    next({ name: 'login' }); // 未登录则跳转到登录页面
  } else {
    next(); // 允许跳转
  }
});

export default router
